/**
 * 史诗钥匙地下城 词缀
 */
const Router = require('@koa/router');
const {
  MythicKeystoneAffixesIndex: AffixIndex,
  MythicKeyStoneAffix: Affix
} = require('../db/schemas')

const { Language } = require('../env');
const { genResponse, raw$project } = require('../utils');

const router = new Router({ prefix: '/affix' })

router.get('/index', async (ctx) => {
  const results = await AffixIndex.aggregate([
    {
      $project: {
        affixes: {
          $map: {
            input: '$affixes',
            as: 'a',
            in: {
              id: '$$a.id',
              name: `$$a.name.${Language}`
            }
          }
        },
      }
    },
  ])
  ctx.body = genResponse(true, results[0].affixes)
})

router.get('/', async (ctx) => {
  const results = await Affix.aggregate([
    {
      $project: {
        _id: 0,
        id: 1,
        name: `$name.${Language}`,
        description: `$description.${Language}`,
        updated_at: 1,
        raw: '$$ROOT'
      }
    },
    raw$project,
  ])
  ctx.body = genResponse(true, results)
})

module.exports = router